import request from "@/utils/request";

const CATEGORY_BASE_URL = "/api/admin";

const CategoryAPI = {

  getAllCategory(){
    return request<any, CategoryInfo[]>({
      url: `${CATEGORY_BASE_URL}/getAllCategory`,
      method: "get",
    });
  },

  checkUpdCategoryName(category_id: number, category_name: string) {
    // 接口返回字段：{code,data,msg} ,其中  request<any, data的类型>
    return request<any, CategoryInfo>({
      url: `${CATEGORY_BASE_URL}/checkCategoryName`,
      params: {category_id, category_name},
      method: "get",
    });
  },

  addSubmitCategory(category: CategoryInfo) {
    return request<any, {}>({
      url: `${CATEGORY_BASE_URL}/category`,
      data: category,
      method: "post",
    });
  },

  editSubmitCategory(category: CategoryInfo) {
    return request<any, {}>({
      url: `${CATEGORY_BASE_URL}/category`,
      data: category,
      method: "put",
    });
  },

  getCategoryInfoById(category_id: number) {
    return request<any, CategoryInfo>({
      url: `${CATEGORY_BASE_URL}/category`,
      params: {category_id},
      method: "get",
    })
  },

  getCategoryInfo({ page, pageSize, category_name, is_show, start_date, end_date }: QueryCategory) {
    return request<any, CategoryPageData>({
      url: `${CATEGORY_BASE_URL}/categoryList`,
      params: {page, pageSize, category_name, is_show, start_date, end_date},
      method: "get",
    });
  },

  deleteCategory(category_id: number) {
    return request<any, {}>({
      url: `${CATEGORY_BASE_URL}/category`,
      params: {category_id},
      method: "delete",
    })
  },

  delBatchCategory(category_ids: string) {
    return request<any, {}>({
      url: `${CATEGORY_BASE_URL}/delBatchCategory`,
      data: {category_ids}, // // 服务器接收： req.body
      method: "delete",
    });
  },
}

export default CategoryAPI

export interface QueryCategory {
  page: number;
  pageSize: number;
  category_name?: string;
  is_show?: number | string;
  start_date?: string;
  end_date?: string;
}

export interface CategoryPageData {
  category: CategoryInfo[];
  count: number;
}

export interface CategoryInfo {
  category_id?: number;
  category_name: string;
  nickname?: string;
  sort: number;
  is_show: number | boolean;
  add_date: string;
  intro: string;
  admin_id: number;
}
